<?php
class TranslateController extends AdminAppController{
    function index()
    {
        $this->loadModel('Admin.Translate');
        $translates = $this->Translate->get_translate();
        $this->set('translates', $translates);
    }
    
    function add()
    {
        $this->loadModel('Admin.Language');
        $this->loadModel('Admin.Translate');
        
        $list_language = $this->Language->get_language();
        $this->set('list_language', $list_language);
        
        if ($this->request->is('post')) {
            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                $data = array(
                        'key_word' => $this->request->data['Translate']['key_word'],
                        'translate' => $this->request->data['Translate']['translate_'.$lang_code],
                        'lang_code' => $lang_code,
                        );
                $this->Translate->add($data);
            }
            $this->Session->setFlash(
                                'Thêm mới thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
            
            $this->redirect(array("controller" => "translate", 
                                  "action" => "index"));
        }
    }
    
    function edit($translate_id = "")
    {
        if($translate_id == ""){
            $this->redirect(array("controller" => "translate", 
                                  "action" => "index")
                            );
            exit;
        }
        
        $this->set('translate_id', $translate_id);
        $this->loadModel('Admin.Language');
        $this->loadModel('Admin.Translate');


        $trans_info = $this->Translate->get_translate_by_id($translate_id);
        $this->set('trans_info', $trans_info);
        
        $list_language = $this->Language->get_language();
        $this->set('list_language', $list_language);
        
        
        if ($this->request->is('post')) {
            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                $data = array(
                        'translate_id' => $translate_id,
                        'key_word' => $this->request->data['Translate']['key_word'],
                        'translate' => $this->request->data['Translate']['translate_'.$lang_code],
                        'lang_code' => $lang_code,
                        );
                $this->Translate->add($data);
            }
            $this->Session->setFlash(
                                'Sửa thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
            
            $this->redirect(array("controller" => "translate", 
                                  "action" => "index"));
        }   
    }
    
}